package cn.com.youjile.service;

import cn.com.youjile.entity.MsgInfo;
import cn.com.youjile.observer.MsgObserver;
import cn.com.youjile.observer.MsgSubject;
import cn.com.youjile.utils.MsgNumUtil;
import cn.com.youjile.utils.ResultUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author youjile
 * @CreateDate 2021/3/12
 * @Describe 心跳
 * @TODO
 */
@Slf4j
@Service
public class Heart extends MsgObserver {

    public Heart(){
        MsgSubject.getInstance().attach(this);
    }

    @Override
    public void update(ChannelHandlerContext ctx, MsgInfo msg) {
        if(msg.getCode() == MsgNumUtil.s_heart) {
            log.debug("server [" + MsgNumUtil.s_heart + "] 心跳:ctx = [" + ctx + "], msg = [" + msg + "]");
            byte[] success = ResultUtil.success("确认收到消息");
            msg.setInfo(success);
            msg.setCode(MsgNumUtil.success);
            ctx.writeAndFlush(msg);
        }
    }
}
